package controllers

import (
	"github.com/astaxie/beego/httplib"
	"encoding/json"
	"github.com/astaxie/beego"
	"xiaoercaidan/publicResources/models"
)

type GoodsController struct {
	beego.Controller
}




// @Title AddCaregory
// @Description 新增分类
// @Param   cate     patch string   true string       "分类json"
// @router /addCategory [get]
func (this *GoodsController) AddCaregory() {

	var cate = this.GetString("cate")

	req := httplib.Get("http://" + beego.AppConfig.String("basehost") + ":" + beego.AppConfig.String("baseport") + "/v1/apis/goods/addCategory")
	req.Param("cate", cate)

	resp, err := req.String()

	if (err == nil) {
		var mapStr = make(map[string]interface{})

		json.Unmarshal([]byte(resp), &mapStr)

		this.Data["json"] = mapStr

	} else {
		var BaseResult = new(models.BaseResult)
		BaseResult.Code = 100
		BaseResult.Msg = "服务异常"
		this.Data["json"] = BaseResult
	}

	this.ServeJSON()

}



// @Title AddGoods
// @Description 新增商品
// @Param   goods     patch string   true string       "商品json"
// @router /addGoods [get]
func (this *GoodsController) AddGoods() {

	var goods = this.GetString("goods")

	req := httplib.Get("http://" + beego.AppConfig.String("basehost") + ":" + beego.AppConfig.String("baseport") + "/v1/apis/goods/addGoods")
	req.Param("goods", goods)

	resp, err := req.String()

	if (err == nil) {
		var mapStr = make(map[string]interface{})

		json.Unmarshal([]byte(resp), &mapStr)

		this.Data["json"] = mapStr

	} else {
		var BaseResult = new(models.BaseResult)
		BaseResult.Code = 100
		BaseResult.Msg = "服务异常"
		this.Data["json"] = BaseResult
	}

	this.ServeJSON()

}






// @Title GetCategory
// @Description 查询分类，父ID为0则查询所有分类
// @Param   parentId    patch int64  true  string       "父类分ID"
// @Param   shopId    patch int64  true  string       "店铺ID"
// @router /category/query [get]
func (this *GoodsController) GetCategory() {

	var parentId = this.GetString("parentId")
	var shopId = this.GetString("shopId")

	req := httplib.Get("http://" + beego.AppConfig.String("basehost") + ":" + beego.AppConfig.String("baseport") + "/v1/apis/goods/queryCategory")
	req.Param("parentId", parentId)
	req.Param("shopId", shopId)

	resp, err := req.String()

	if (err == nil) {
		var mapStr = make(map[string]interface{})

		json.Unmarshal([]byte(resp), &mapStr)

		this.Data["json"] = mapStr

	}

	this.ServeJSON()

}


// @Title GetCategory
// @Description 查询商品
// @Param   name    patch int64  true  string       "商品关键字"
// @Param   categoryId    patch int64  true  string       "分类ID"
// @Param   shopId    patch int64  true  string       "门店ID"
// @router /query [get]
func (this *GoodsController) GetGoods() {

	var name = this.GetString("name")
	var shopId = this.GetString("shopId")
	var categoryId = this.GetString("categoryId")

	req := httplib.Get("http://" + beego.AppConfig.String("basehost") + ":" + beego.AppConfig.String("baseport") + "/v1/apis/goods/queryGoods")
	req.Param("name", name)
	req.Param("categoryId", categoryId)
	req.Param("shopId", shopId)

	resp, err := req.String()

	if (err == nil) {
		var mapStr = make(map[string]interface{})

		json.Unmarshal([]byte(resp), &mapStr)

		this.Data["json"] = mapStr

	}

	this.ServeJSON()

}


// @Title GetShopList
// @Param   goods     patch string   true string       "商品ID列表，以'，'分割"
// @Param   shopId     patch string   true string       "门店ID"
// @Description 店铺删除
// @router /goodsDel [get]
func (this *GoodsController) GoodsDel() {

	goods := this.GetString("goods")
	shopId := this.GetString("shopId")

	req := httplib.Get("http://" + beego.AppConfig.String("basehost") + ":" + beego.AppConfig.String("baseport") + "/v1/apis/goods/delGoods")
	req.Param("goods", goods)
	req.Param("shopId", shopId)

	resp, err := req.String()

	if (err == nil) {
		var mapStr map[string]interface{}

		json.Unmarshal([]byte(resp), &mapStr)

		this.Data["json"] = mapStr

	} else {
		var BaseResult = new(models.BaseResult)
		BaseResult.Code = 100
		BaseResult.Msg = "服务异常"
		this.Data["json"] = BaseResult
	}

	this.ServeJSON()

}





// @Title CategoryUpdate
// @Param   goods     patch string   true string       "商品json"
// @Description 分类修改
// @router /goodsUpdate [get]
func (this *GoodsController) GoodsUpdate() {

	goods := this.GetString("goods")

	req := httplib.Get("http://" + beego.AppConfig.String("basehost") + ":" + beego.AppConfig.String("baseport") + "/v1/apis/goods/updateGoods")
	req.Param("goods", goods)

	resp, err := req.String()

	if (err == nil) {
		var mapStr map[string]interface{}

		json.Unmarshal([]byte(resp), &mapStr)

		this.Data["json"] = mapStr

	} else {
		var BaseResult = new(models.BaseResult)
		BaseResult.Code = 100
		BaseResult.Msg = "服务异常"
		this.Data["json"] = BaseResult
	}

	this.ServeJSON()

}




// @Title CategoryUpdate
// @Param   cate     patch string   true string       "分类json"
// @Description 分类修改
// @router /categoryUpdate [get]
func (this *GoodsController) CategoryUpdate() {

	cate := this.GetString("cate")

	req := httplib.Get("http://" + beego.AppConfig.String("basehost") + ":" + beego.AppConfig.String("baseport") + "/v1/apis/goods/categoryUpdate")
	req.Param("cate", cate)

	resp, err := req.String()

	if (err == nil) {
		var mapStr map[string]interface{}

		json.Unmarshal([]byte(resp), &mapStr)

		this.Data["json"] = mapStr

	} else {
		var BaseResult = new(models.BaseResult)
		BaseResult.Code = 100
		BaseResult.Msg = "服务异常"
		this.Data["json"] = BaseResult
	}

	this.ServeJSON()

}






